home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-06 / an105x.zip / IPX.C < prev    next >
C/C++ Source or Header  |  1991-04-09  |  4KB  |  192 lines

  1. /*****************************************************************************
  2. * IPX.C
  3. *
  4. * 91-04-05 Matt Hagen, Novell, Inc.
  5. *****************************************************************************/
  6.  
  7. #include "ipx.h"
  8.  
  9. /*****************************************************************************
  10. * AllocateIPXResources
  11. *****************************************************************************/
  12.  
  13. SESSION *AllocateIPXResources(
  14.     WORD socketValue,
  15.     BYTE semaphoreValue,
  16.     BYTE rxCount,
  17.     LONG rxDataSize,
  18.     BYTE txCount,
  19.     LONG txDataSize)
  20. {
  21.     int a;
  22.     SESSION *s;
  23.     ECBHOLD *h;
  24.  
  25.     s=malloc(sizeof(SESSION));
  26.     if(s==NULL)
  27.         goto error0;
  28.  
  29.     s->semaphore=OpenLocalSemaphore(semaphoreValue);
  30.     if(s->semaphore==NULL)
  31.         goto error1;
  32.  
  33.     s->socket=socketValue;
  34.     s->rxHead=NULL;
  35.     s->rxList=NULL;
  36.     s->txList=NULL;
  37.  
  38.     if(IpxOpenSocket(&s->socket)!=ESUCCESS)
  39.         goto error2;
  40.  
  41.     for(a=0;a<rxCount;a++)
  42.     {
  43.         h=AllocatePacket(rxDataSize,s->socket,s->semaphore,&s->rxHead);
  44.         if(h==NULL)
  45.             goto error3;
  46.  
  47.         h->fLink=s->rxList;
  48.         s->rxList=h;
  49.  
  50.         if(IpxReceive(NULL,h->ecb)!=ESUCCESS)
  51.             goto error3;
  52.     }
  53.  
  54.     for(a=0;a<txCount;a++)
  55.     {
  56.         h=AllocatePacket(txDataSize,s->socket,NULL,NULL);
  57.         if(h==NULL)
  58.             goto error4;
  59.  
  60.         h->fLink=s->txList;
  61.         s->txList=h;
  62.     }
  63.  
  64.     return(s);
  65.  
  66. error4:;
  67.     while(s->txList!=NULL)
  68.     {
  69.         h=s->txList;
  70.         s->txList=h->fLink;
  71.         DeallocatePacket(h);
  72.     }
  73.  
  74. error3:;
  75.     IpxCloseSocket(s->socket);
  76.  
  77.     while(s->rxList!=NULL)
  78.     {
  79.         h=s->rxList;
  80.         s->rxList=h->fLink;
  81.         DeallocatePacket(h);
  82.     }
  83.  
  84. error2:;
  85.     CloseLocalSemaphore(s->semaphore);
  86.  
  87. error1:;
  88.     free(s);
  89.  
  90. error0:;
  91.     return(NULL);
  92. }
  93.  
  94. /*****************************************************************************
  95. * DeallocateIPXResources
  96. *****************************************************************************/
  97.  
  98. void DeallocateIPXResources(
  99.     SESSION *s)
  100. {
  101.     ECBHOLD *h;
  102.  
  103.     while(s->txList!=NULL)
  104.     {
  105.         h=s->txList;
  106.         s->txList=h->fLink;
  107.         DeallocatePacket(h);
  108.     }
  109.  
  110.     IpxCloseSocket(s->socket);
  111.  
  112.     while(s->rxList!=NULL)
  113.     {
  114.         h=s->rxList;
  115.         s->rxList=h->fLink;
  116.         DeallocatePacket(h);
  117.     }
  118.  
  119.     CloseLocalSemaphore(s->semaphore);
  120.  
  121.     free(s);
  122. }
  123.  
  124. /*****************************************************************************
  125. * AllocatePacket
  126. *****************************************************************************/
  127.  
  128. ECBHOLD *AllocatePacket(
  129.     LONG dataSize,
  130.     WORD socket,
  131.     LONG semaphore,
  132.     IPX_ECB **head)
  133. {
  134.     ECBHOLD *h;
  135.     IPX_ECB *ecb;
  136.  
  137.     h=malloc(sizeof(ECBHOLD));
  138.     if(h==NULL)
  139.         goto error0;
  140.  
  141.     ecb=malloc(sizeof(IPX_ECB));
  142.     if(ecb==NULL)
  143.         goto error1;
  144.  
  145.     ecb->fragList[0].fragAddress=malloc(sizeof(IPX_HEADER));
  146.     if(ecb->fragList[0].fragAddress==NULL)
  147.         goto error2;
  148.  
  149.     ecb->fragList[1].fragAddress=malloc(dataSize);
  150.     if(ecb->fragList[1].fragAddress==NULL)
  151.         goto error3;
  152.  
  153.     ecb->queueHead=head;
  154.     ecb->semHandle=semaphore;
  155.     ecb->socket=socket;
  156.     ecb->fragCount=2;
  157.     ecb->fragList[0].fragSize=sizeof(IPX_HEADER);
  158.     ecb->fragList[1].fragSize=dataSize;
  159.     ((IPX_HEADER *)(ecb->fragList[0].fragAddress))->packetType=NULL;
  160.  
  161.     h->ecb=ecb;
  162.     return(h);
  163.  
  164. error3:;
  165.     free(ecb->fragList[0].fragAddress);
  166.  
  167. error2:;
  168.     free(ecb);
  169.  
  170. error1:;
  171.     free(h);
  172.  
  173. error0:;
  174.     return(NULL);
  175. }
  176.  
  177. /*****************************************************************************
  178. * DeallocatePacket
  179. *****************************************************************************/
  180.  
  181. void DeallocatePacket(
  182.     ECBHOLD *h)
  183. {
  184.     free(h->ecb->fragList[1].fragAddress);
  185.     free(h->ecb->fragList[0].fragAddress);
  186.     free(h->ecb);
  187.     free(h);
  188. }
  189.  
  190. /****************************************************************************/
  191. /****************************************************************************/
  192.